#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "DS1302.h"
#include "Key.h"
#include "Timer0.h"
#include "IR.h"

unsigned char TimeSetFlashFlag;
unsigned char TimeSetSelect,MODE0;

unsigned char GetMODE0()
{
	return MODE0;
}

/**
  * @brief  时间显示，模式0
  */
void TimeShow(void)
{

		DS1302_ReadTime();
		LCD_ShowNum(1,1,DS1302_Time[0],2);
		LCD_ShowString(1,3,"-");
		LCD_ShowNum(1,4,DS1302_Time[1],2);
		LCD_ShowString(1,6,"-");
		LCD_ShowNum(1,7,DS1302_Time[2],2);
		LCD_ShowString(1,13,WeekGet(DS1302_Time[6]));
//		LCD_ShowNum(1,11,DS1302_Time[6],1);
		LCD_ShowNum(2,1,DS1302_Time[3],2);
		LCD_ShowString(2,3,":");
		LCD_ShowNum(2,4,DS1302_Time[4],2);
		LCD_ShowString(2,6,":");
		LCD_ShowNum(2,7,DS1302_Time[5],2);
}


/**
  * @brief  模式1,时间设置,TimeSetFlashFlag
  */
void TimeSet(unsigned char KeyNum)
{
	
	//按下按键二，后移动光标,其中星期能设置，但是会自动匹配
	if(KeyNum == 2)
	{
		TimeSetSelect++;
		TimeSetSelect%=6;
//		LCD_ShowNum(2,14,TimeSetSelect,1);
	}
	
	//前移动光标
	if(KeyNum == 3)
	{
		TimeSetSelect--;
		if(TimeSetSelect<0 ||TimeSetSelect>10)
		{
				TimeSetSelect = 5;
		}
//		LCD_ShowNum(2,14,TimeSetSelect,1);
	}
	
	//值加
	if(KeyNum == 4)
	{
		DS1302_Time[TimeSetSelect]++;
		if(DS1302_Time[0]>99){DS1302_Time[0]=0;}
		if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
		}
		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
		}
		else if(DS1302_Time[1]==2)
		{
			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
			{
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
			}
			else
			{
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
			}
		}
		if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
		if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
		if(DS1302_Time[5]>59){DS1302_Time[5]=0;}
	}
	
	
	
	//值减
	if(KeyNum == 5)
	{
		DS1302_Time[TimeSetSelect]--;
		if(DS1302_Time[0]<0){DS1302_Time[0]=99;}
		if(DS1302_Time[1]<1){DS1302_Time[1]=12;}
		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
		}
		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
		{
			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
		}
		else if(DS1302_Time[1]==2)
		{
			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
			}
			else
			{
				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
			}
		}
		if(DS1302_Time[3]<0){DS1302_Time[3]=23;}
		if(DS1302_Time[4]<0){DS1302_Time[4]=59;}
		if(DS1302_Time[5]<0){DS1302_Time[5]=59;}
	}
	//自动匹配星期
	WeekSet();
	
	//进行设置显示，让被选中的进行闪烁
	if(TimeSetSelect == 0 && TimeSetFlashFlag == 1){LCD_ShowString(1,1,"  ");}
	else{LCD_ShowNum(1,1,DS1302_Time[0],2);}
	LCD_ShowString(1,3,"-");
	if(TimeSetSelect == 1 && TimeSetFlashFlag == 1){LCD_ShowString(1,4,"  ");}
	else{LCD_ShowNum(1,4,DS1302_Time[1],2);}
	LCD_ShowString(1,6,"-");
	if(TimeSetSelect == 2 && TimeSetFlashFlag == 1){LCD_ShowString(1,7,"  ");}
	else{LCD_ShowNum(1,7,DS1302_Time[2],2);}
	if(TimeSetSelect == 6 && TimeSetFlashFlag == 1){LCD_ShowString(1,11,"    ");}
	else{LCD_ShowString(1,13,WeekGet(DS1302_Time[6]));}
	if(TimeSetSelect == 3 && TimeSetFlashFlag == 1){LCD_ShowString(2,1,"  ");}
	else{LCD_ShowNum(2,1,DS1302_Time[3],2);}
	LCD_ShowString(2,3,":");
	if(TimeSetSelect == 4 && TimeSetFlashFlag == 1){LCD_ShowString(2,4,"  ");}
	else{LCD_ShowNum(2,4,DS1302_Time[4],2);}
	LCD_ShowString(2,6,":");
	if(TimeSetSelect == 5 && TimeSetFlashFlag == 1){LCD_ShowString(2,7,"  ");}
	else{LCD_ShowNum(2,7,DS1302_Time[5],2);}
//	LCD_ShowNum(2,10,TimeSetFlashFlag,2);
	
	//确认修改
	if(KeyNum == 7)
	{
		DS1302_SetTime();
		MODE0 = 1;
	}
	else
	{
		MODE0 = 0;
	}
}


/**
  * @brief  闪烁
  */
unsigned char TimeSetFlash()
{
	TimeSetFlashFlag = !TimeSetFlashFlag;
	return TimeSetFlashFlag;
}


/**
  * @brief  模式1,时间设置
  */
//void TimeSet(unsigned char Command)
//{
//	unsigned char TimeSetSelect;
//	//红外遥控重构按键
//	
//	//按下按键二，移动光标,其中星期能设置，但是会自动匹配
//	if(Command == 1)
//	{
//		TimeSetSelect++;
//		TimeSetSelect%=6;
//		LCD_ShowNum(2,14,TimeSetSelect,1);
//	}
//	
//	//按键3，加
//	if(Command == 2)
//	{
//		DS1302_Time[TimeSetSelect]++;
//		if(DS1302_Time[0]>99){DS1302_Time[0]=0;}
//		if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
//		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
//			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
//		{
//			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
//			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
//		}
//		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
//		{
//			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
//			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
//		}
//		else if(DS1302_Time[1]==2)
//		{
//			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
//			{
//				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
//				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
//			}
//			else
//			{
//				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
//				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
//			}
//		}
//		if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
//		if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
//		if(DS1302_Time[5]>59){DS1302_Time[5]=0;}
//	}
//	
//	
//	
//	//按键4，减
//	if(Command == 3)
//	{
//		DS1302_Time[TimeSetSelect]--;
//		if(DS1302_Time[0]<0){DS1302_Time[0]=99;}
//		if(DS1302_Time[1]<1){DS1302_Time[1]=12;}
//		if(DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 || DS1302_Time[1]==8
//			 || DS1302_Time[1]==10 || DS1302_Time[1]==12)
//		{
//			if(DS1302_Time[2]<1){DS1302_Time[2]=31;}
//			if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
//		}
//		else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
//		{
//			if(DS1302_Time[2]<1){DS1302_Time[2]=30;}
//			if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
//		}
//		else if(DS1302_Time[1]==2)
//		{
//			if((DS1302_Time[0]%4==0 && DS1302_Time[0]%100!=0)|| DS1302_Time[0]%400==0)
//			{
//				if(DS1302_Time[2]<1){DS1302_Time[2]=29;}
//				if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
//			}
//			else
//			{
//				if(DS1302_Time[2]<1){DS1302_Time[2]=28;}
//				if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
//			}
//		}
//		if(DS1302_Time[3]<0){DS1302_Time[3]=23;}
//		if(DS1302_Time[4]<0){DS1302_Time[4]=59;}
//		if(DS1302_Time[5]<0){DS1302_Time[5]=59;}
//	}
//	//自动匹配星期
//	WeekSet();
//	
//	//进行设置显示，让被选中的进行闪烁
//	if(TimeSetSelect == 0 && TimeSetFlash() == 1){LCD_ShowString(1,1,"  ");}
//	else{LCD_ShowNum(1,1,DS1302_Time[0],2);}
//	LCD_ShowString(1,3,"-");
//	if(TimeSetSelect == 1 && TimeSetFlashFlag == 1){LCD_ShowString(1,4,"  ");}
//	else{LCD_ShowNum(1,4,DS1302_Time[1],2);}
//	LCD_ShowString(1,6,"-");
//	if(TimeSetSelect == 2 && TimeSetFlashFlag == 1){LCD_ShowString(1,7,"  ");}
//	else{LCD_ShowNum(1,7,DS1302_Time[2],2);}
//	if(TimeSetSelect == 6 && TimeSetFlashFlag == 1){LCD_ShowString(1,11,"    ");}
//	else{LCD_ShowString(1,13,WeekGet(DS1302_Time[6]));}
//	if(TimeSetSelect == 3 && TimeSetFlashFlag == 1){LCD_ShowString(2,1,"  ");}
//	else{LCD_ShowNum(2,1,DS1302_Time[3],2);}
//	LCD_ShowString(2,3,":");
//	if(TimeSetSelect == 4 && TimeSetFlashFlag == 1){LCD_ShowString(2,4,"  ");}
//	else{LCD_ShowNum(2,4,DS1302_Time[4],2);}
//	LCD_ShowString(2,6,":");
//	if(TimeSetSelect == 5 && TimeSetFlashFlag == 1){LCD_ShowString(2,7,"  ");}
//	else{LCD_ShowNum(2,7,DS1302_Time[5],2);}
////	LCD_ShowNum(2,10,TimeSetFlashFlag,2);
//}
